// pages/bar/bar.js
var timer
var isTimer = false
Page({

  /**
   * 页面的初始数据
   */
  data: {
    randMode: "随机",
    isRand: true,
    percent: [8, 16, 25, 34, 42, 50, 58, 66, 75, 86, 92, 100],
    color: ["#f00", "#f90", "#ff0", "#9f0", "#0f0", "#0f9", "#0ff", "#09f", "#00f", "#90f", "#f0f", "#f09"]
  },
  p: [8, 16, 25, 34, 42, 50, 58, 66, 75, 86, 92, 100],
  c: ["#f00", "#f90", "#ff0", "#9f0", "#0f0", "#0f9", "#0ff", "#09f", "#00f", "#90f", "#f0f", "#f09"],
  randColor: function () {
    var r = Math.floor(Math.random() * 255)
    var g = Math.floor(Math.random() * 255)
    var b = Math.floor(Math.random() * 255)
    var str = "rgb(" + r + "," + g + "," + b + ")"
    return str
  },
  setRand: function (e) {
    if (e.detail.value) {
      this.setData({
        isRand: true
      })
      this.setData({
        randMode: "随机"
      })
    } else {
      this.setData({
        isRand: false
      })
      this.setData({
        randMode: "依次"
      })
    }
  },
  randWave: function () {
    for (var i = 0; i < 12; i++) {
      var rnd = Math.floor(Math.random() * 101)
      this.p[i] = rnd
      this.c[i] = this.randColor()
    }
    this.setData({
      percent: this.p
    })
    this.setData({
      color: this.c
    })
  },
  stepWave: function () {
    for (var i = 0; i < 11; i++) {
      this.p[i] = this.p[i + 1]
      this.c[i] = this.c[i + 1]
    }
    this.p[11] = Math.floor(Math.random() * 101)
    this.c[11] = this.randColor()
    this.setData({
      percent: this.p
    })
    this.setData({
      color: this.c
    })
  },
  manualWave: function () {
    clearTimeout(timer)
    if (this.data.isRand) {
      this.randWave()
    } else {
      this.stepWave()
    }
  },
  autoWave: function () {
    clearTimeout(timer)
    if (this.data.isRand) {
      this.randWave()
    } else {
      this.stepWave()
    }
    timer = setTimeout(this.autoWave, 300)
  },
  resetWave: function () {
    clearTimeout(timer)
    var p = [8, 16, 25, 34, 42, 50, 58, 66, 75, 86, 92, 100]
    var c = ["#f00", "#f90", "#ff0", "#9f0", "#0f0", "#0f9", "#0ff", "#09f", "#00f", "#90f", "#f0f", "#f09"]
    this.setData({
      percent: p
    })
    this.setData({
      color: c
    })
    this.p = p
    this.c = c
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearTimeout(timer)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearTimeout(timer)
  },

})